package espacokidsadm.web.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import espacokidsadm.modelo.entity.Cargo;
import espacokidsadm.modelo.negocio.CargoRN;


@FacesConverter(forClass=Cargo.class)
public class CargoConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		if(value != null && value.trim().length() > 0){
			int idCargo = Integer.parseInt(value);
			try{
				return new CargoRN().getById(idCargo);
			}catch(Exception e){
				throw new ConverterException("Não foi possivel encontrar o CARGO pelo id = " + idCargo + "." + e.getMessage());
			}
		}
		return null;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {
		if(value != null){
			Cargo cargo = (Cargo) value;
			return String.valueOf(cargo.getId());
		}
		return "";
	}

}
